﻿using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Zenject;

public class WeaponSelectUIManager : MonoBehaviourSingleton<WeaponSelectUIManager>
{
    [Inject] private WeaponService _weaponService;
    [Inject] private GameManager _gameManager;
    [Inject] private DiContainer _container;

    [Header("武器选择GridUI")] [SerializeField]
    private GameObject parent;

    [SerializeField] private GameObject weaponSelectUIPrefab;
    private List<WeaponSelectUI> _weaponSelectUIList = new List<WeaponSelectUI>();
    [Header("武器展示UI")] [SerializeField] private Image weaponAvatarImage;
    [SerializeField] private TextMeshProUGUI weaponNameText;
    [SerializeField] private TextMeshProUGUI isLongText;
    [SerializeField] private TextMeshProUGUI weaponDescribeText;

    [Header("角色展示UI")] [SerializeField] private Image avatarImage;
    [SerializeField] private TextMeshProUGUI roleNameText;
    [SerializeField] private TextMeshProUGUI roleDescribeText;


    [Header("难度选择")] [SerializeField] private GameObject difficultySelectUI;


    public void UpdateRoleInfoUI()
    {
        Role currentRole = _gameManager.currentRoleBaseData;
        if (currentRole.unlock)
        {
            //解锁
            roleNameText.text = currentRole.name;
            roleDescribeText.text = currentRole.describe.Replace("\\n", "\n");
            avatarImage.sprite = Resources.Load<Sprite>(currentRole.avatar);
        }
        else
        {
            //未解锁
            roleNameText.text = "？？？";
            roleDescribeText.text = currentRole.unlockConditions;
            avatarImage.sprite = Resources.Load<Sprite>("Image/UI/锁");
        }
    }

    /**
     * 初始化武器选择的GridUI
     */
    private void Start()
    {
        foreach (Weapon weapon in _weaponService.SelectAllWeapon())
        {
            GameObject weaponUI = _container.InstantiatePrefab(weaponSelectUIPrefab, parent.transform);
            weaponUI.GetComponent<WeaponSelectUI>()?.InitUI(weapon);
            _weaponSelectUIList.Add(weaponUI.GetComponent<WeaponSelectUI>());
        }

        _weaponSelectUIList[0].UpdateWeaponInfoUI();
    }


    /**
     * 切换难度选择UI
     */
    public void SwitchDifficultySelectUI()
    {
        //切换武器选择UI
        SwitchUIManager.Instance.SwitchUI(difficultySelectUI);
    }

    public void UpdateWeaponInfoUI(Weapon weapon)
    {
        //解锁
        weaponNameText.text = weapon.name;
        weaponDescribeText.text = weapon.describe.Replace("\\n", "\n");
        weaponAvatarImage.sprite = Resources.Load<Sprite>(weapon.avatar);
        if (weapon.isLong)
        {
            isLongText.text = "远程";
        }
        else
        {
            isLongText.text = "近程";
        }
    }


    /**
 * 随机选择一个角色
 */
    public void RandomWeapon()
    {
        //随机选择一个
        _weaponSelectUIList[Random.Range(0, _weaponSelectUIList.Count)].SelectWeapon();
    }
}